package com.wazzapps.sdk;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.tapjoy.TapjoyConstants;
import com.wazzapps.sdk.api.CustomEvent;
import com.wazzapps.sdk.api.Event;
import com.wazzapps.sdk.api.Service;
import com.wazzapps.sdk.utils.Tools;

import org.json.JSONException;

import java.util.Locale;

public class WazzApplicationInstance {
    public static final String INSTALL_KEY = "install_event_sent";
    private static final String LOG_TAG = "WazzSdk";
    public static final String REFERRER_SEND_KEY = "referrer_event_sent";
    public static final String USER_KEY = "uniqueUserId";
    public static final String USER_PREFS = "sdK_wazz_preferences";
    private Context context;
    private boolean isDebug = false;
    private boolean isInited = false;
    private String sdkKey = null;
    private WazzUserInfo userInfo = null;

    public WazzApplicationInstance(Context context) {
        this.context = context;
    }

    public void init(String str, Boolean bool, boolean z, String str2) {
        this.sdkKey = str;
        this.isDebug = bool.booleanValue();
        boolean z2 = bool.booleanValue() || !WazzChecker.isWrongAppSignature(this.context);
        this.isInited = z2;
        if (!z2) {
            Log.e(LOG_TAG, "Wrong apk.");
            return;
        }
        Log.d(LOG_TAG, "isDebug: " + bool + ", isPersonalizedEnabled: " + z);
        this.userInfo = new WazzUserInfo(this.context, z);
        Context applicationContext = this.context.getApplicationContext();
        try {
            String string = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 128).metaData.getString("WazzappsSDKLocale");
            if (TextUtils.isEmpty(str2)) {
                str2 = Tools.getCountryFromNetwork(this.context);
            }
            if (TextUtils.isEmpty(str2)) {
                str2 = applicationContext.getResources().getConfiguration().locale.getCountry();
            }
            if (string == null) {
                string = Locale.getDefault().getLanguage();
            }
            Service.getInstance().init(str, this.context.getPackageName(), string, str2, this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0).versionCode, this.userInfo);
            Service.getInstance().addEvent(new Event(Event.Type.APP_LOAD));
            Log.d(LOG_TAG, "WazzappsSDK inited");
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG_TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
        } catch (NullPointerException e2) {
            Log.e(LOG_TAG, "Failed to load meta-data, NullPointer: " + e2.getMessage());
        }
        final SharedPreferences sharedPreferences = this.context.getSharedPreferences(USER_PREFS, 0);
        if (!sharedPreferences.getBoolean(INSTALL_KEY, false)) {
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putBoolean(INSTALL_KEY, true);
            edit.apply();
            Service.getInstance().addEvent(new Event(Event.Type.APP_INSTALL));
        }
        if (!sharedPreferences.getBoolean(REFERRER_SEND_KEY, false) && sharedPreferences.getString(WazzInstallReceiver.REFERRER_KEY, null) == null) {
            WazzInstallReceiver.getReferrerFromApi(this.context, new Runnable() { // from class: com.wazzapps.sdk.WazzApplicationInstance.1
                @Override // java.lang.Runnable
                public void run() {
                    SharedPreferences.Editor edit2 = sharedPreferences.edit();
                    edit2.putBoolean(WazzApplicationInstance.REFERRER_SEND_KEY, true);
                    edit2.apply();
                    Event event = new Event(Event.Type.REFERRER_RECEIVED);
                    try {
                        String string2 = sharedPreferences.getString(WazzInstallReceiver.REFERRER_KEY, "unknown");
                        event.details.put(TapjoyConstants.TJC_REFERRER, string2);
                        Log.d(WazzApplicationInstance.LOG_TAG, "WA Send referrer: " + string2);
                    } catch (JSONException unused) {
                    }
                    Service.getInstance().addEvent(event);
                }
            });
        }
    }

    public Context getContext() {
        return this.context;
    }

    public String getReferrer() {
        return this.context.getSharedPreferences(USER_PREFS, 0).getString(WazzInstallReceiver.REFERRER_KEY, "");
    }

    public boolean isInited() {
        return this.isInited;
    }

    public boolean isDebug() {
        return this.isDebug;
    }

    public String getUser() {
        WazzUserInfo wazzUserInfo = this.userInfo;
        if (wazzUserInfo != null) {
            return wazzUserInfo.getInfo();
        }
        return null;
    }

    public void logEvent(String... strArr) {
        Service.getInstance().addEvent(new CustomEvent(strArr));
    }
}